//
//  NetworkTool.swift
//  WBDemo
//
//  Created by Meng on 17/2/8.
//  Copyright © 2017年 demo. All rights reserved.
//

import UIKit
import AFNetworking

class NetworkTool: AFHTTPSessionManager {
    
    static let shareInstance: NetworkTool = {
        
        let baseURL = URL(string: "https://api.weibo.com/")!
        
        let instance = NetworkTool(baseURL: baseURL, sessionConfiguration: URLSessionConfiguration.default)
        
        instance.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "application/json") as? Set
        
        return instance
    }()
    
    // MARK: - 外部控制方法
    // MARK: 加载微博数据
    func loadStatuses(since_id: String, max_id: String, finished: @escaping (_ dictArr: [[String: AnyObject]]?, _ error: Error?)->()) {
        guard let userAccount = UserAccount.readUserAccount() else {
            MYLog("必须授权之后才能获取微博数据")
            return
        }
        let path = "2/statuses/home_timeline.json"
//        let maxid = (max_id != "0") ? "\(Double(max_id)! - 1)" : max_id
        let parameters = ["access_token": userAccount.access_token!, "since_id": since_id, "max_id": max_id, "count": "20"]
//        MYLog(parameters)
        get(path, parameters: parameters, progress: nil, success: { (task, objc) in
            guard let arr = (objc as! [String: AnyObject])["statuses"] as? [[String: AnyObject]] else {
                finished(nil, NSError(domain: "com.mx", code: 10000, userInfo: ["message": "没有获取到数据"]) as Error)
                return
            }
            finished(arr, nil)
        }) { (task, error) in
            finished(nil, error)
        }
    }
    
    func sendStatus(status: String, image: UIImage?, finished: @escaping (_ objc: Any?, _ error: Error?)->()) {
        guard let userAccount = UserAccount.readUserAccount() else {
            MYLog("必须授权之后才能获取微博数据")
            return
        }
        if image != nil {
            
            let path = "2/statuses/upload.json"
            let parameters = ["access_token": userAccount.access_token!, "status": status]
            post(path, parameters: parameters, constructingBodyWith: { (formData: AFMultipartFormData) in
                let data = UIImagePNGRepresentation(image!)!
                formData.appendPart(withFileData: data, name: "pic", fileName: "123.png", mimeType: "image/png")
            }, progress: nil, success: { (task: URLSessionDataTask, objc: Any?) in
                finished(objc, nil)
            }) { (task: URLSessionDataTask?, error: Error) in
                finished(nil, error)
            }
            return
        }
        
        let path = "2/statuses/update.json"
        let parameters = ["access_token": userAccount.access_token!, "status": status]
        post(path, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, objc: Any?) in
            finished(objc, nil)
        }) { (task: URLSessionDataTask?, error: Error) in
            finished(nil, error)
        }
    }
}
